<?php

namespace app\common\model;

use app\common\model\Common as CommonModel;
use think\facade\Db;
/**
 * 后台用户权限model类
 * @package Home\Model
 */
class AdminRole extends CommonModel
{
//    protected $name = 'admin_role';
    protected $pk = 'role_id';

    // index页面数据获取
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 20;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where('role_id','<>',1)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());//返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);//返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['role_name']) && !empty($post['role_name'])){
            $where[] = ['role_name','like','%'.$post['role_name'].'%'];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        foreach($list as $k=>$v){
            $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }
        return $list;
    }

    //添加和修改操作
    public function handle($param){
        $act = $param['act'];

        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];


        if(!isset($param['meun']) || empty($param['meun']) ){
            $result['msg'] = '请选择权限';
            return $result;
        }

        $data['role_name'] = $param['role_name'];
        $data['act_list'] = implode(',',$param['meun']);


        if($act == 'add'){
            $data['add_time'] = time();
            $r = $this->insertGetId($data);
            $id = $r;
            $ty = '添加';
        }else{
            $data['update_time'] = time();
            $r = $this->where('role_id','=',$param['role_id'])->update($data);
            $id = $param['role_id'];
            $ty = '修改';
        }

        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        adminLog("角色管理".$ty,$id);
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        return $result;
    }

}